#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : gtmsAutoTestProject 
@File    : printer_ip_use_server.py
@IDE     : PyCharm 
@Author  : Mario
@Date    : 2022/5/7 16:39 
@Describe: 获取IP对应打印机信息接口业务实现
"""
from Config.yamlRead import brandCode_cfg
from Comm.tools.toolsControl import ToolsHandler
from APIs.middleApi.setup.printer.printer_manage_api import PrintManagerApi


def printer_ip_use(IP):
    """
    获取IP对应打印机信息
    :param IP: 打印机IP
    :return:
    """
    environment = ['sit', 'uat', 'prod']  # 环境列表
    printer_info_list = []  # 定义空列表存放匹配到的打印机信息
    for x in environment:  # 循环多个环境
        brandCode = brandCode_cfg  # 取出yaml配置文件的品牌编码信息
        middle_requests = PrintManagerApi(x)  # 中台请求方式实例化
        for y in brandCode:  # 循环多个品牌
            # 调用打印机管理分页接口获取打印机列表
            printer_info = middle_requests.pos_printer_manage_page(y)
            # 取出打印机列表长度进行循环
            printer_len = ToolsHandler.get_json_result(printer_info, '$.data.list')
            for z in range(len(printer_len)):
                # 取出当前循环打印机的打印类型,1代表网络打印机
                printer_mode = ToolsHandler.get_json_result(printer_info, '$.data.list[{}].printerMode'.format(z))
                if printer_mode == 1:  # 判断打印机是否为网络打印机,如果是就取出打印机的IP
                    printer_ip = ToolsHandler.get_json_result(printer_info, '$.data.list[{}].printerIp'.format(z))
                    if printer_ip == IP:  # 判断取出来的IP是否与传入的值是否一致,如果一致就往空列表插入数据
                        printer_name = ToolsHandler.get_json_result(printer_info, '$.data.list[{}].printerName'.format(z))
                        printer_info_list.append("在{}环境,{}品牌发现对应打印机.IP:{},打印机名称:{}".format(x, y, IP, printer_name))
    if printer_info_list:  # 判断空列表数据是否为空,不为空就返回列表数据
        return printer_info_list
    else:
        warning_message = '当前IP未绑定网络打印机,请检查后重试~'
        return warning_message
